package com.ueffort.ad.platform;

import android.app.Activity;
import android.content.Intent;
import android.view.ViewGroup;

import com.miui.zeus.mimo.sdk.BannerAd;
import com.miui.zeus.mimo.sdk.InterstitialAd;
import com.miui.zeus.mimo.sdk.MimoSdk;
import com.miui.zeus.mimo.sdk.RewardVideoAd;
import com.ueffort.ad.AdSDK;
import com.ueffort.ad.AdProvider;
import com.ueffort.ad.action.PreloadAction;
import com.ueffort.ad.action.ShowAction;
import com.unity3d.player.UnityPlayer;

import java.util.Dictionary;
import java.util.Hashtable;


public class MiAdProvider implements AdProvider {

    private static MiAdProvider instance=new MiAdProvider();

    private Activity activity;
    private ViewGroup viewGroup;

    private static Dictionary<String, BannerAd> mPreloadBanner=new Hashtable<>();
    private static Dictionary<String, InterstitialAd> mPreloadInterstitial=new Hashtable<>();
    private static Dictionary<String, RewardVideoAd> mPreloadReward=new Hashtable<>();

    public static MiAdProvider Instance(){
        return instance;
    }
    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        return false;
    }

    @Override
    public void Init(String appId, Activity activity, UnityPlayer unityPlayer) {
        this.activity = activity;
        this.viewGroup = unityPlayer;
        MimoSdk.init(activity, appId);
    }

    @Override
    public void SetDebugMode(boolean isTest) {
        MimoSdk.setDebugOn(isTest);
        MimoSdk.setStagingOn(isTest);
    }

    @Override
    public void Preload(AdSDK.AdType adType, String unitId, PreloadAction action) {
        switch(adType){
            case Banner:
                BannerAd bannerAd = new BannerAd(activity);
                bannerAd.loadAd(unitId, new BannerAd.BannerLoadListener() {
                    @Override
                    public void onBannerAdLoadSuccess() {
                        action.OnSuccess();
                        mPreloadBanner.put(unitId, bannerAd);
                    }
                    @Override
                    public void onAdLoadFailed (int error, String errorMsg) {
                        action.OnFail(errorMsg);
                    }
                });
                break;
            case Interstitial:
                InterstitialAd mInterstitialAd = new InterstitialAd(activity); // thisActivity
                mInterstitialAd.loadAd(unitId, new InterstitialAd.InterstitialAdLoadListener() {
                    @Override
                    public void onAdLoadSuccess() {
                        action.OnSuccess();
                        mPreloadInterstitial.put(unitId, mInterstitialAd);
                    }
                    @Override
                    public void onAdLoadFailed(int errorCode, String errorMsg) {
                        action.OnFail(errorMsg);
                    }
                });
                break;
            case Reward:
                RewardVideoAd mRewardVideoAd = new RewardVideoAd(activity);
                mRewardVideoAd.loadAd(unitId, new RewardVideoAd.RewardVideoLoadListener() {
                    @Override
                    public void onAdLoadSuccess() {
                        action.OnSuccess();
                        mPreloadReward.put(unitId, mRewardVideoAd);
                    }
                    @Override
                    public void onAdLoadFailed(int errorCode, String errorMsg) {
                        action.OnFail(errorMsg);
                    }
                });
                break;
            default:
        }
    }

    @Override
    public boolean IsLoaded(AdSDK.AdType adType, String unitId) {
        switch(adType){
            case Banner:
                return mPreloadBanner.get(unitId) != null;
            case Interstitial:
                return mPreloadInterstitial.get(unitId) != null;
            case Reward:
                return mPreloadReward.get(unitId) != null;
            default:
        }
        return false;
        
    }

    @Override
    public void Show(AdSDK.AdType adType, String unitId, ShowAction action) {
        switch(adType){
            case Banner:
                BannerAd bannerAd = mPreloadBanner.get(unitId);
                if (bannerAd == null) {
                    action.OnShowFail("no load");
                    return;
                }
                bannerAd.showAd(viewGroup, new BannerAd.BannerInteractionListener() {
                    @Override
                    public void onAdClick() {
                        action.OnClick();
                        action.OnClose(false);
                        bannerAd.destroy();
                    }
                    @Override
                    public void onAdShow() {
                    }
                    @Override
                    public void onAdDismiss() {
                        action.OnClose(false);
                        bannerAd.destroy();
                    }
                    @Override
                    public void onRenderSuccess() {
                        action.OnShowSuccess();
                    }
                    @Override
                    public void onRenderFail(int errorCode, String errorMsg) {
                        action.OnShowFail(errorMsg);
                        bannerAd.destroy();
                    }
                });
                mPreloadBanner.remove(unitId);
                break;
            case Interstitial:
                InterstitialAd mInterstitialAd = mPreloadInterstitial.get(unitId);
                if (mInterstitialAd == null) {
                    action.OnShowFail("no load");
                    return;
                }
                mInterstitialAd.show(new InterstitialAd.InterstitialAdInteractionListener() {
                    @Override
                    public void onAdClick() {
                        action.OnClick();
                        action.OnClose(false);
                        mInterstitialAd.destroy();
                    }
                    @Override
                    public void onAdShow() {
                        action.OnShowSuccess();
                    }
                    @Override
                    public void onAdClosed() {
                        action.OnClose(false);
                        mInterstitialAd.destroy();
                    }
                    @Override
                    public void onRenderFail(int errorCode, String errorMsg) {
                        action.OnShowFail(errorMsg);
                        mInterstitialAd.destroy();
                    }
                });
                mPreloadInterstitial.remove(unitId);
                break;
            case Reward:
                RewardVideoAd mRewardVideoAd = mPreloadReward.get(unitId);
                if (mRewardVideoAd == null) {
                    action.OnShowFail("no load");
                    return;
                }
                mRewardVideoAd.showAd(new RewardVideoAd.RewardVideoInteractionListener() {
                    boolean reward = false;
                    @Override
                    public void onAdPresent() {
                        action.OnVideoShow();
                    }
                    @Override
                    public void onAdClick() {
                        action.OnClick();
                        reward = true;
                        action.OnClose(reward);
                        mRewardVideoAd.recycle();
                    }
                    @Override
                    public void onAdDismissed() {
                        action.OnClose(reward);
                        mRewardVideoAd.recycle();
                    }
                    @Override
                    public void onAdFailed(String errorMsg) {
                        action.OnShowFail(errorMsg);
                        mRewardVideoAd.recycle();
                    }
                    @Override
                    public void onVideoStart() {
                        action.OnShowSuccess();
                    }
                    @Override
                    public void onVideoPause() {
                        
                    }
                    @Override
                    public void onVideoComplete() {
                        reward = true;
                    }
                    @Override
                    public void onPicAdEnd() {
                        reward = true;
                    }
                });
                mPreloadInterstitial.remove(unitId);

            default:
        }
    }
}
